import 'package:fish_redux/fish_redux.dart';
import 'action.dart';
import 'state.dart';
import '../../app_files.dart';

Effect<UserCenterState> buildEffect() {
  return combineEffects(<Object, Effect<UserCenterState>>{
    UserCenterAction.onTap: _onTap,
    UserCenterAction.onGasTap: _onGasTap,
    UserCenterAction.onLogout: _onLogout,
    Lifecycle.initState: _initState,
  });
}

void _initState(Action action, Context<UserCenterState> ctx) async {
  // bus.on('setGasUser', (arg) {
  //   ctx.state.isGsUser = true;
  //   ctx.dispatch(UserCenterActionCreator.onAction());
  // });
  //
  // bus.on('refreshMain', (arg) {
  //   ctx.state.isGsUser = false;
  //   ctx.dispatch(UserCenterActionCreator.onAction());
  // });
  ResultData configRes = await CommonNetWork.getSystemConfig();
  if (configRes.success) {
    List data = configRes.data;
    Map config = data.first;
    List value = config['ConfigValue'];
    value.forEach((element) {
      if (element['Key'] == 'manual') {
        ctx.state.url = element['Value'];
      }
    });
    ctx.dispatch(UserCenterActionCreator.onAction());
  }
}

void _onTap(Action action, Context<UserCenterState> ctx) {
  if (action.payload == 0)
    CommonUtils.pushRoute(ctx.context, RoutePath.userInfoView);
  if (action.payload == 1)
    CommonUtils.pushRoute(ctx.context, RoutePath.userAccountView);
  if (action.payload == 2)
    CommonUtils.pushRoute(ctx.context, RoutePath.userTypeView,
        arguments: {'isChanged': true});
}

void _onLogout(Action action, Context<UserCenterState> ctx) {
  CommonUtils.showAlertView(ctx.context, "注销成功！").then((r) {
    bus.emit('gomain');
    bus.emit('logout');
    GlobalStore.store.dispatch(GlobalActionCreator.onUserLogout());
    CommonUtils.gotoLoginView(ctx.context);
  });
}

void _onGasTap(Action action, Context<UserCenterState> ctx) {
  print(action.payload);
  switch (action.payload) {
    case '我的资料':
      CommonUtils.pushRoute(ctx.context, RoutePath.userEditInfoView);
      break;
    case '设置':
      CommonUtils.pushRoute(ctx.context, RoutePath.settingView);
      break;
    case '燃气户号管理':
      CommonUtils.pushRoute(ctx.context, RoutePath.gasUserAccountView);
      break;
    case '意见反馈':
      CommonUtils.pushRoute(ctx.context, RoutePath.feedbackView);
      break;
    case '客服电话':
      CommonUtils.pushRoute(ctx.context, RoutePath.hotlineView);
      break;
    case '我的账号':
      CommonUtils.pushRoute(ctx.context, RoutePath.userInfoAccountView);
      break;
  }
}
